| string (1) | basic_string& replace (size_type pos, size_type len, const basic_string& str);basic_string& replace (iterator i1, iterator i2, const basic_string& str); |
|---|---|
| substring (2) | basic_string& replace (size_type pos, size_type len, const basic_string& str, size_type subpos, size_type sublen); |
| c-string (3) | basic_string& replace (size_type pos, size_type len, const charT* s);basic_string& replace (iterator i1, iterator i2, const charT* s); |
| buffer (4) | basic_string& replace (size_type pos, size_type len, const charT* s, size_type n);basic_string& replace (iterator i1, iterator i2, const charT* s, size_type n); |
| fill (5) | basic_string& replace (size_type pos, size_type len, size_type n, charT c);basic_string& replace (iterator i1, iterator i2, size_type n, charT c); |
| range (6) | template <class InputIterator> basic_string& replace (iterator i1, iterator i2, InputIterator first, InputIterator last); |
| string (1) | basic_string& replace (size_type pos, size_type len, const basic_string& str);basic_string& replace (const_iterator i1, const_iterator i2, const basic_string& str); |
|---|---|
| substring (2) | basic_string& replace (size_type pos, size_type len, const basic_string& str, size_type subpos, size_type sublen); |
| c-string (3) | basic_string& replace (size_type pos, size_type len, const charT* s);basic_string& replace (const_iterator i1, const_iterator i2, const charT* s); |
| buffer (4) | basic_string& replace (size_type pos, size_type len, const charT* s, size_type n);basic_string& replace (const_iterator i1, const_iterator i2, const charT* s, size_type n); |
| fill (5) | basic_string& replace (size_type pos, size_type len, size_type n, charT c);basic_string& replace (const_iterator i1, const_iterator i2, size_type n, charT c); |
| range (6) | template <class InputIterator> basic_string& replace (const_iterator i1, const_iterator i2, InputIterator first, InputIterator last); |
| initializer list (7) | basic_string& replace (const_iterator i1, const_iterator i2, initializer_list<charT> il); |
| string (1) | basic_string& replace (size_type pos, size_type len, const basic_string& str);basic_string& replace (const_iterator i1, const_iterator i2, const basic_string& str); |
|---|---|
| substring (2) | basic_string& replace (size_type pos, size_type len, const basic_string& str, size_type subpos, size_type sublen = npos); |
| c-string (3) | basic_string& replace (size_type pos, size_type len, const charT* s);basic_string& replace (const_iterator i1, const_iterator i2, const charT* s); |
| buffer (4) | basic_string& replace (size_type pos, size_type len, const charT* s, size_type n);basic_string& replace (const_iterator i1, const_iterator i2, const charT* s, size_type n); |
| fill (5) | basic_string& replace (size_type pos, size_type len, size_type n, charT c);basic_string& replace (const_iterator i1, const_iterator i2, size_type n, charT c); |
| range (6) | template <class InputIterator> basic_string& replace (const_iterator i1, const_iterator i2, InputIterator first, InputIterator last); |
| initializer list (7) | basic_string& replace (const_iterator i1, const_iterator i2, initializer_list<charT> il); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// replacing in a string
#include <iostream>
#include <string>
int main ()
{
std::string base="this is a test string.";
std::string str2="n example";
std::string str3="sample phrase";
std::string str4="useful.";
// replace signatures used in the same order as described above:
// Using positions: 0123456789*123456789*12345
std::string str=base; // "this is a test string."
str.replace(9,5,str2); // "this is an example string." (1)
str.replace(19,6,str3,7,6); // "this is an example phrase." (2)
str.replace(8,10,"just a"); // "this is just a phrase." (3)
str.replace(8,6,"a shorty",7); // "this is a short phrase." (4)
str.replace(22,1,3,'!'); // "this is a short phrase!!!" (5)
// Using iterators: 0123456789*123456789*
str.replace(str.begin(),str.end()-3,str3); // "sample phrase!!!" (1)
str.replace(str.begin(),str.begin()+6,"replace"); // "replace phrase!!!" (3)
str.replace(str.begin()+8,str.begin()+14,"is coolness",7); // "replace is cool!!!" (4)
str.replace(str.begin()+12,str.end()-4,4,'o'); // "replace is cooool!!!" (5)
str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful." (6)
std::cout << str << '\n';
return 0;
}
replace is useful.